package com.spider.admin.store.primary.model;

import com.spider.admin.conf.validator.UniqueValue;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotEmpty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;

@Getter
@Setter
@Validated
@Entity
@Table(name = "t_application_info")

@Accessors(chain = true)

@Data
@EqualsAndHashCode(callSuper = true)
public class AppInfo extends SuperVO {

    @Id
    @Column(name = "pid")
    private String pid;

    @Column(name = "app_name")
    @NotEmpty(message = "应用名称不能为空", groups = {Save.class})
    @UniqueValue(domainClass = AppInfo.class, fieldName = "appName", message = "应用名称已经存在", groups = {Save.class})
    private String appName;

    @Column(name = "tenant_id")
    @NotEmpty(message = "归属租户不能为空", groups = {Save.class})
    private String tenantId;


    @Column(name = "cloud_id")
    @NotEmpty(message = "发布服务云不能为空", groups = {Save.class})
    private String cloudId;

    @Column(name = "app_url")
    //相同的发布地址会生成集群
//    @UniqueValue(domainClass = AppInfo.class, fieldName = "appUrl", message = "应用发布地址已被占用", groups = {Save.class,Update.class})
    private String appUrl;

    @Transient
    private String appUri;

    public String getAppUri() {
        if (cloudInfo == null) {
            return this.appUrl;
        }
        if (this.appUrl == null || this.appUrl.equals("/")) {
            this.appUrl = "";
        }
        this.appUri = cloudInfo == null ? this.appUrl : (cloudInfo.getCloudBasePath() + this.appUrl);
        if (!this.appUri.startsWith("http://") && !this.appUri.startsWith("https://")) {
            this.appUri = "http://" + this.appUri;
        }
        return this.appUri;
    }

    @Transient
//    @JsonIgnore
    private CloudInfo cloudInfo;
    @Column(name = "app_desc")
    private String appDesc;

    @Column(name = "app_original_url")
    @NotEmpty(message = "应用服务原始服务器地址URL不能为空", groups = {Save.class})
    @UniqueValue(domainClass = AppInfo.class, fieldName = "appOriginalUrl", message = "应用服务已经存在", groups = {Save.class})
    private String appOriginalUrl;

    @Column(name = "app_status")
    private String appStatus;

}

